package org.sn.jdish.spring.springboot.base;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class MyExceptionHandler {

	@ExceptionHandler(value = Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.addObject("exception", e);
		mav.addObject("url", req.getRequestURL());
		mav.setViewName("error");
		return mav;
	}

	@ExceptionHandler(value = MyException.class)
	@ResponseBody
	public Result<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
		Result<String> result = new Result<String>();
		result.setMessage(e.getMessage());
		result.setCode(Result.ERROR);
		result.setData("Some Data");
		result.setUrl(req.getRequestURL().toString());
		return result;
	}

}
